home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_repr.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  13KB  |  396 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''
  5.   Test cases for the repr module
  6.   Nick Mathewson
  7. '''
  8. import sys
  9. import os
  10. import unittest
  11. from test.test_support import run_unittest
  12. from repr import repr as r
  13.  
  14. def nestedTuple(nesting):
  15.     t = ()
  16.     for i in range(nesting):
  17.         t = (t,)
  18.     
  19.     return t
  20.  
  21.  
  22. class ReprTests(unittest.TestCase):
  23.     
  24.     def test_string(self):
  25.         eq = self.assertEquals
  26.         eq(r('abc'), "'abc'")
  27.         eq(r('abcdefghijklmnop'), "'abcdefghijklmnop'")
  28.         s = 'a' * 30 + 'b' * 30
  29.         expected = repr(s)[:13] + '...' + repr(s)[-14:]
  30.         eq(r(s), expected)
  31.         eq(r('"\''), repr('"\''))
  32.         s = '"' * 30 + "'" * 100
  33.         expected = repr(s)[:13] + '...' + repr(s)[-14:]
  34.         eq(r(s), expected)
  35.  
  36.     
  37.     def test_container(self):
  38.         array = array
  39.         import array
  40.         deque = deque
  41.         import collections
  42.         eq = self.assertEquals
  43.         eq(r(()), '()')
  44.         eq(r((1,)), '(1,)')
  45.         eq(r((1, 2, 3)), '(1, 2, 3)')
  46.         eq(r((1, 2, 3, 4, 5, 6)), '(1, 2, 3, 4, 5, 6)')
  47.         eq(r((1, 2, 3, 4, 5, 6, 7)), '(1, 2, 3, 4, 5, 6, ...)')
  48.         eq(r([]), '[]')
  49.         eq(r([
  50.             1]), '[1]')
  51.         eq(r([
  52.             1,
  53.             2,
  54.             3]), '[1, 2, 3]')
  55.         eq(r([
  56.             1,
  57.             2,
  58.             3,
  59.             4,
  60.             5,
  61.             6]), '[1, 2, 3, 4, 5, 6]')
  62.         eq(r([
  63.             1,
  64.             2,
  65.             3,
  66.             4,
  67.             5,
  68.             6,
  69.             7]), '[1, 2, 3, 4, 5, 6, ...]')
  70.         eq(r(set([])), 'set([])')
  71.         eq(r(set([
  72.             1])), 'set([1])')
  73.         eq(r(set([
  74.             1,
  75.             2,
  76.             3])), 'set([1, 2, 3])')
  77.         eq(r(set([
  78.             1,
  79.             2,
  80.             3,
  81.             4,
  82.             5,
  83.             6])), 'set([1, 2, 3, 4, 5, 6])')
  84.         eq(r(set([
  85.             1,
  86.             2,
  87.             3,
  88.             4,
  89.             5,
  90.             6,
  91.             7])), 'set([1, 2, 3, 4, 5, 6, ...])')
  92.         eq(r(frozenset([])), 'frozenset([])')
  93.         eq(r(frozenset([
  94.             1])), 'frozenset([1])')
  95.         eq(r(frozenset([
  96.             1,
  97.             2,
  98.             3])), 'frozenset([1, 2, 3])')
  99.         eq(r(frozenset([
  100.             1,
  101.             2,
  102.             3,
  103.             4,
  104.             5,
  105.             6])), 'frozenset([1, 2, 3, 4, 5, 6])')
  106.         eq(r(frozenset([
  107.             1,
  108.             2,
  109.             3,
  110.             4,
  111.             5,
  112.             6,
  113.             7])), 'frozenset([1, 2, 3, 4, 5, 6, ...])')
  114.         eq(r(deque([
  115.             1,
  116.             2,
  117.             3,
  118.             4,
  119.             5,
  120.             6,
  121.             7])), 'deque([1, 2, 3, 4, 5, 6, ...])')
  122.         eq(r({ }), '{}')
  123.         d = {
  124.             'alice': 1,
  125.             'bob': 2,
  126.             'charles': 3,
  127.             'dave': 4 }
  128.         eq(r(d), "{'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}")
  129.         d['arthur'] = 1
  130.         eq(r(d), "{'alice': 1, 'arthur': 1, 'bob': 2, 'charles': 3, ...}")
  131.         eq(r(array('i')), "array('i', [])")
  132.         eq(r(array('i', [
  133.             1])), "array('i', [1])")
  134.         eq(r(array('i', [
  135.             1,
  136.             2])), "array('i', [1, 2])")
  137.         eq(r(array('i', [
  138.             1,
  139.             2,
  140.             3])), "array('i', [1, 2, 3])")
  141.         eq(r(array('i', [
  142.             1,
  143.             2,
  144.             3,
  145.             4])), "array('i', [1, 2, 3, 4])")
  146.         eq(r(array('i', [
  147.             1,
  148.             2,
  149.             3,
  150.             4,
  151.             5])), "array('i', [1, 2, 3, 4, 5])")
  152.         eq(r(array('i', [
  153.             1,
  154.             2,
  155.             3,
  156.             4,
  157.             5,
  158.             6])), "array('i', [1, 2, 3, 4, 5, ...])")
  159.  
  160.     
  161.     def test_numbers(self):
  162.         eq = self.assertEquals
  163.         eq(r(123), repr(123))
  164.         eq(r(0x7BL), repr(0x7BL))
  165.         eq(r(1.0 / 3), repr(1.0 / 3))
  166.         n = 0xAL ** 100
  167.         expected = repr(n)[:18] + '...' + repr(n)[-19:]
  168.         eq(r(n), expected)
  169.  
  170.     
  171.     def test_instance(self):
  172.         eq = self.assertEquals
  173.         i1 = ClassWithRepr('a')
  174.         eq(r(i1), repr(i1))
  175.         i2 = ClassWithRepr('x' * 1000)
  176.         expected = repr(i2)[:13] + '...' + repr(i2)[-14:]
  177.         eq(r(i2), expected)
  178.         i3 = ClassWithFailingRepr()
  179.         eq(r(i3), '<ClassWithFailingRepr instance at %x>' % id(i3))
  180.         s = r(ClassWithFailingRepr)
  181.         self.failUnless(s.startswith('<class '))
  182.         self.failUnless(s.endswith('>'))
  183.         self.failUnless(s.find('...') == 8)
  184.  
  185.     
  186.     def test_file(self):
  187.         fp = open(unittest.__file__)
  188.         self.failUnless(repr(fp).startswith("<open file '%s', mode 'r' at 0x" % unittest.__file__))
  189.         fp.close()
  190.         self.failUnless(repr(fp).startswith("<closed file '%s', mode 'r' at 0x" % unittest.__file__))
  191.  
  192.     
  193.     def test_lambda(self):
  194.         self.failUnless(repr((lambda x: x)).startswith('<function <lambda'))
  195.  
  196.     
  197.     def test_builtin_function(self):
  198.         eq = self.assertEquals
  199.         eq(repr(hash), '<built-in function hash>')
  200.         self.failUnless(repr(''.split).startswith('<built-in method split of str object at 0x'))
  201.  
  202.     
  203.     def test_xrange(self):
  204.         import warnings as warnings
  205.         eq = self.assertEquals
  206.         eq(repr(xrange(1)), 'xrange(1)')
  207.         eq(repr(xrange(1, 2)), 'xrange(1, 2)')
  208.         eq(repr(xrange(1, 2, 3)), 'xrange(1, 4, 3)')
  209.  
  210.     
  211.     def test_nesting(self):
  212.         eq = self.assertEquals
  213.         eq(r([
  214.             [
  215.                 [
  216.                     [
  217.                         [
  218.                             [
  219.                                 []]]]]]]), '[[[[[[[]]]]]]]')
  220.         eq(r([
  221.             [
  222.                 [
  223.                     [
  224.                         [
  225.                             [
  226.                                 [
  227.                                     []]]]]]]]), '[[[[[[[...]]]]]]]')
  228.         eq(r(nestedTuple(6)), '(((((((),),),),),),)')
  229.         eq(r(nestedTuple(7)), '(((((((...),),),),),),)')
  230.         eq(r({
  231.             nestedTuple(5): nestedTuple(5) }), '{((((((),),),),),): ((((((),),),),),)}')
  232.         eq(r({
  233.             nestedTuple(6): nestedTuple(6) }), '{((((((...),),),),),): ((((((...),),),),),)}')
  234.         eq(r([
  235.             [
  236.                 [
  237.                     [
  238.                         [
  239.                             [
  240.                                 { }]]]]]]), '[[[[[[{}]]]]]]')
  241.         eq(r([
  242.             [
  243.                 [
  244.                     [
  245.                         [
  246.                             [
  247.                                 [
  248.                                     { }]]]]]]]), '[[[[[[[...]]]]]]]')
  249.  
  250.     
  251.     def test_buffer(self):
  252.         x = buffer('foo')
  253.         self.failUnless(repr(x).startswith('<read-only buffer for 0x'))
  254.  
  255.     
  256.     def test_cell(self):
  257.         pass
  258.  
  259.     
  260.     def test_descriptors(self):
  261.         eq = self.assertEquals
  262.         eq(repr(dict.items), "<method 'items' of 'dict' objects>")
  263.         
  264.         class C:
  265.             
  266.             def foo(cls):
  267.                 pass
  268.  
  269.  
  270.         x = staticmethod(C.foo)
  271.         self.failUnless(repr(x).startswith('<staticmethod object at 0x'))
  272.         x = classmethod(C.foo)
  273.         self.failUnless(repr(x).startswith('<classmethod object at 0x'))
  274.  
  275.  
  276.  
  277. def touch(path, text = ''):
  278.     fp = open(path, 'w')
  279.     fp.write(text)
  280.     fp.close()
  281.  
  282.  
  283. def zap(actions, dirname, names):
  284.     for name in names:
  285.         actions.append(os.path.join(dirname, name))
  286.     
  287.  
  288.  
  289. class LongReprTest(unittest.TestCase):
  290.     
  291.     def setUp(self):
  292.         longname = 'areallylongpackageandmodulenametotestreprtruncation'
  293.         self.pkgname = os.path.join(longname)
  294.         self.subpkgname = os.path.join(longname, longname)
  295.         os.mkdir(self.pkgname)
  296.         touch(os.path.join(self.pkgname, '__init__' + os.extsep + 'py'))
  297.         os.mkdir(self.subpkgname)
  298.         touch(os.path.join(self.subpkgname, '__init__' + os.extsep + 'py'))
  299.         self.here = os.getcwd()
  300.         sys.path.insert(0, self.here)
  301.  
  302.     
  303.     def tearDown(self):
  304.         actions = []
  305.         os.path.walk(self.pkgname, zap, actions)
  306.         actions.append(self.pkgname)
  307.         actions.sort()
  308.         actions.reverse()
  309.         for p in actions:
  310.             if os.path.isdir(p):
  311.                 os.rmdir(p)
  312.                 continue
  313.             os.remove(p)
  314.         
  315.         del sys.path[0]
  316.  
  317.     
  318.     def test_module(self):
  319.         eq = self.assertEquals
  320.         touch(os.path.join(self.subpkgname, self.pkgname + os.extsep + 'py'))
  321.         areallylongpackageandmodulenametotestreprtruncation = areallylongpackageandmodulenametotestreprtruncation
  322.         import areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation
  323.         eq(repr(areallylongpackageandmodulenametotestreprtruncation), "<module '%s' from '%s'>" % (areallylongpackageandmodulenametotestreprtruncation.__name__, areallylongpackageandmodulenametotestreprtruncation.__file__))
  324.         eq(repr(sys), "<module 'sys' (built-in)>")
  325.  
  326.     
  327.     def test_type(self):
  328.         eq = self.assertEquals
  329.         touch(os.path.join(self.subpkgname, 'foo' + os.extsep + 'py'), 'class foo(object):\n    pass\n')
  330.         foo = foo
  331.         import areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation
  332.         eq(repr(foo.foo), "<class '%s.foo'>" % foo.__name__)
  333.  
  334.     
  335.     def test_object(self):
  336.         pass
  337.  
  338.     
  339.     def test_class(self):
  340.         touch(os.path.join(self.subpkgname, 'bar' + os.extsep + 'py'), 'class bar:\n    pass\n')
  341.         bar = bar
  342.         import areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation
  343.         self.failUnless(repr(bar.bar).startswith('<class %s.bar at 0x' % bar.__name__))
  344.  
  345.     
  346.     def test_instance(self):
  347.         touch(os.path.join(self.subpkgname, 'baz' + os.extsep + 'py'), 'class baz:\n    pass\n')
  348.         baz = baz
  349.         import areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation
  350.         ibaz = baz.baz()
  351.         self.failUnless(repr(ibaz).startswith('<%s.baz instance at 0x' % baz.__name__))
  352.  
  353.     
  354.     def test_method(self):
  355.         eq = self.assertEquals
  356.         touch(os.path.join(self.subpkgname, 'qux' + os.extsep + 'py'), 'class aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n    def amethod(self): pass\n')
  357.         qux = qux
  358.         import areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation
  359.         eq(repr(qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod), '<unbound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod>')
  360.         iqux = qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
  361.         self.failUnless(repr(iqux.amethod).startswith('<bound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod of <%s.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa instance at 0x' % (qux.__name__,)))
  362.  
  363.     
  364.     def test_builtin_function(self):
  365.         pass
  366.  
  367.  
  368.  
  369. class ClassWithRepr:
  370.     
  371.     def __init__(self, s):
  372.         self.s = s
  373.  
  374.     
  375.     def __repr__(self):
  376.         return 'ClassWithLongRepr(%r)' % self.s
  377.  
  378.  
  379.  
  380. class ClassWithFailingRepr:
  381.     
  382.     def __repr__(self):
  383.         raise Exception('This should be caught by Repr.repr_instance')
  384.  
  385.  
  386.  
  387. def test_main():
  388.     run_unittest(ReprTests)
  389.     if os.name != 'mac':
  390.         run_unittest(LongReprTest)
  391.     
  392.  
  393. if __name__ == '__main__':
  394.     test_main()
  395.  
  396.